/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cip.esymons.eventos.converters;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.cip.esymons.general.be.PersonaNatural;
import org.cip.esymons.eventos.beans.GeneralController;

import org.cip.esymons.eventos.beans.UnidadMedidaController;

/**
 *
 * @author oscar
 */
public class GeneralConverter implements Converter{
@Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null || value.length() == 0) {
            return null;
        }

        Integer id = new Integer(value);
        GeneralController controller1 = (GeneralController) context.getApplication().getELResolver().getValue(context.getELContext(), null, "GeneralController");
        UnidadMedidaController controller = (UnidadMedidaController) context.getApplication().getELResolver().getValue(context.getELContext(), null, "UnidadMedidaController");
        try {
            //controller1.ge
            return controller.getDelegate().buscarUnidadMedida(id);
        } catch (Exception ex) {
            Logger.getLogger(EventoConverters.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof  PersonaNatural) {
            PersonaNatural p = (PersonaNatural) value;
//            return p.getPerestadoCivil() == null ? "" : p.getPerestadoCivil().toString();
            return "";
        } else {
            throw new IllegalArgumentException("object " + value + " is of type " + value.getClass().getName() + "; expected type: org.cip.esymons.eventos.be");
        }
    }

}
